﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyFactory : Singleton<EnemyFactory>
{
    public int waveIndex = 0;
    public int enemyCount = 0;
    public bool finish;

    public EnemyWave[] waves;

	void Start () {
        waves[waveIndex].StartSpawn();
        //UIManager.Instance.SetWave(waveIndex + 1);
    }

    void Update () {
        if (finish) return;

        //判断当前波次完成,并且敌人全部死亡
        if(waves[waveIndex].FinishSpawn() && enemyCount==0)
        {
            //开启下一个波次
            waveIndex++;
            //UIManager.Instance.SetWave(waveIndex + 1);

            if (waveIndex < waves.Length)
            {
                waves[waveIndex].StartSpawn();
            }
            else{
                finish = true;
                Debug.Log("出兵结束");
            }
        } else{
            waves[waveIndex].UpdateSpawn();//当前波次的更新
        }
    }


    /// <summary>
    /// 创建敌人的工厂
    /// </summary>
    /// <param name="item">单个敌人的item</param>
    public static void CreateEnemy(WaveItem item)
    {
        GameObject prefab = Resources.Load<GameObject>(item.prefabName);
        var go = Instantiate(prefab, Instance.transform.position, Quaternion.identity);
        go.GetComponent<EnemyHP>().Init(item.maxHp);

        Instance.enemyCount++;
    }
}
